/**
 * @param {number[]} nums
 * @return {number}
 *
 * 1 2 3 4 5
 * 0 1 2 3 4
 */

var findMagicIndex = function (nums) {
  let index = nums.length
  let start = 0,
    end = nums.length
  let mid
  while (start < end) {
    mid = Math.floor((end - start) / 2)
    if (mid == nums[mid]) {
      index = mid
    }
    if (mid < nums[mid]) {
      end = mid - 1
    } else {
      start = mid + 1
    }
  }
  return index==nums.length?-1:index
}
